#include<graphics.h>
#include "E_Point.h"
#include<cmath>
using namespace std;

#define N 3

int func(int n);
double Bernstein(double t, int i);
int main() {


	HWND hwnd = initgraph(500, 500);
	setbkcolor(WHITE);
	cleardevice();
	setorigin(250, 250);
	setaspectratio(1, -1);
	setlinecolor(BLACK);
	setlinestyle(PS_SOLID, 2);
	E_Point P[N+1] = { {-100,50},{0,100},{100,50},{150,-20} };
	moveto(P[0].x, P[0].y);
	lineto(P[1].x, P[1].y);
	lineto(P[2].x, P[2].y);
	lineto(P[3].x, P[3].y);
	moveto(P[0].x, P[0].y);
	for (double t = 0; t < 1; t += 0.000001) {
		double x = 0, y = 0;
		for (int i = 0; i <= N; i++) {
			x += Bernstein(t, i) * P[i].x;
			y += Bernstein(t, i) * P[i].y;
		}
		lineto(x, y);
	}
	_getwch();
	closegraph();
	return 0;
}

int func(int n) {
	if (n == 0 || n == 1) {
		return 1;
	}
	else {
		return n * func(n - 1);
	}
}

double Bernstein(double t, int i) {
	return (func(N) * pow(t, i) * pow((1 - t), (N - i))) / (func(i) * func(N - i));
}
